package org.ling.xuan.action;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxOptional;

/**
 * lx取消标记动作
 *
 * @author liting50684
 * @date 2023/08/01
 */
public class LxUnMarkAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
        if (StrUtil.isNotBlank(basePath) && StrUtil.isNotBlank(filePath)) {
            MarkFileDao.unMark(basePath, filePath, null);
            LoggerUtil.log(e.getProject(), "取消标记成功:{}", filePath);
        } else {
            LoggerUtil.log(e.getProject(), "无效的unmark操作..");
        }
    }

    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().unMarkCondition(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
